/* Simple JavaScript Inheritance
 * By John Resig https://johnresig.com/  jquery 
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function () {
    var initializing = false, fnTest = /xyz/.test(function () {
        xyz;
    }) ? /\b_super\b/ : /.*/;

    // The base Class implementation (does nothing)
    this.Class = function () {};

    // Create a new Class that inherits from this class
    Class.extend = function (prop) {
        var _super = this.prototype;

        // Instantiate a base class (but only create the instance,
        // don't run the init constructor)
        initializing = true;
        var prototype = new this();
        initializing = false;

        // Copy the properties over onto the new prototype
        for (var name in prop) {
            // Check if we're overwriting an existing function
            prototype[name] = typeof prop[name] == "function" &&
                    typeof _super[name] == "function" && fnTest.test(prop[name]) ?
                    (function (name, fn) {
                        return function () {
                            var tmp = this._super;

                            // Add a new ._super() method that is the same method
                            // but on the super-class
                            this._super = _super[name];

                            // The method only need to be bound temporarily, so we
                            // remove it when we're done executing
                            var ret = fn.apply(this, arguments);
                            this._super = tmp;

                            return ret;
                        };
                    })(name, prop[name]) :
                    prop[name];
        }

        // The dummy class constructor
        function Class() {
            // All construction is actually done in the init method
            if (!initializing && this.init)
                this.init.apply(this, arguments);
        }

        // Populate our constructed prototype object
        Class.prototype = prototype;

        // Enforce the constructor to be what we expect
        Class.prototype.constructor = Class;

        // And make this class extendable
        Class.extend = arguments.callee;

        return Class;
    };
})();


/**
 * 组件集合
 * @type type
 */
var COMS = COMS || {
    types: {
        /**
         * init
         * @type String
         */
        0:"init",
        /**
         * html
         * @type String
         */
        1: "html",
        /**
         * image
         * @type String
         */
        2: "image",
        /**
         * images
         * @type String
         */
        3: "images",
//    "4":"",
//    "5":"",
        /**
         * text
         * @type String
         */
        6: "text",
        /**
         * dividingline
         * @type String
         */
        7: "dividingline",
        /**
         * video
         * @type String
         */
        8: "video",
        /**
         * timeline
         * @type String
         */
        9: "timeline",
        /**
         * combination
         * @type String
         */
        10: "combination"
    }
};

/**
 * base组件
 * @type type
 */
var BaseCom = Class.extend({
    type: null,
    data: {},
    init: function () {},
    initData: function () {
        console.log("To Init");
    },
    showEditor: function (option) {
        console.log("COMS:Ready to edit what？");
    },
    calculateImgEditorMaxHeight:function($img){
        var _imgHeight = $img[0].height;
        var _imgWidth = $img[0].width;
//        var _bodyClientHeight = document.body.clientHeight;
        var _screenHeight = window.screen.height;
        var _editImgMaxHeight = "100%";
        if(_imgHeight > _imgWidth){
            _editImgMaxHeight = _screenHeight*0.4;
        }      
        return _editImgMaxHeight;        
    },    
    hideEditor:function(option){
        console.log("COMS:Close / cancel edit");
    },
    reloadPreview: function (option) {
        console.log("COMS:What to re-render？");
    },
    fetchData: function (option) {
        return {};
    },
    addNewImages: function (option) {
        console.log("COMS:Add a new picture for who？");
    },
    changeCropImage: function (option) {
        console.log("COMS:Single graph Multi-map? Who cut the picture?");
    },
    filterSensitiveString: function (s) {
        return s.replace(/<script.*?>.*?<\/script>/ig, '');
//        return s.replace(/<script[^>]*>/ig, '');
    },
    dealVideoSrc: function (s) {
        var _return = {code: 0, content: "目前只支持腾讯、优酷、爱奇艺视频"};
        if (s === "") {
            return _return;
        }
        //是不是优酷、腾讯、爱奇艺的地址 
        var youkuVideoReg = /.+youku.com.+/gi;
        var qqVideoReg = /.+qq.com.+/gi;
        var qiyiVideoReg = /.+qiyi.com.+/gi;
        var videoFrom = null;
        var _iframeSrc = '';
        if (youkuVideoReg.test(s)) {
            videoFrom = "youku";
        } else if (qqVideoReg.test(s)) {
            videoFrom = "qq";
        } else if (qiyiVideoReg.test(s)) {
            videoFrom = "qiyi";
        } else {
            return _return;
        }
        if (videoFrom == null) {
            return _return;
        }

        //处理iframe =>直接返回
        var iframeReg = /.*<iframe.+/gi;
        if (iframeReg.test(s)) {
//            _return.content = s;
//            _return.code = 1;            
//            return _return;
            var _srcReg = /.*<iframe.+src=['"]([^'"]+)['"]\s.+/gi;
            var _srcRegRes = _srcReg.exec(s);
            if (_srcRegRes != null && _srcRegRes[1] != '') {
                var _newIframeSrc = _srcRegRes[1].replace(/https/,'http');                
                _return.content = '<iframe src="' + _newIframeSrc + '" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
                _return.code = 1;
            }            
            return _return;
        }
        //处理embed
        var embedReg = /.*<embed.+/gi;
        if (embedReg.test(s)) {
            _iframeSrc = this.getEmbedToIframeSrc(s, videoFrom);
            if (_iframeSrc != "") {
                _return.content = '<iframe src="' + _iframeSrc + '" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
                _return.code = 1;
            }
            return _return;
        }

        //处理flash http链接
        var httpReg = /^http(s)?:\/\/.+$/gi;
        var testRes = httpReg.test(s);
        if (testRes) {
            _iframeSrc = this.getFlashToIframeSrc(s, videoFrom);
            _return.content = '<iframe src="' + _iframeSrc + '" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
            _return.code = 1;
            return _return;
        }
        //不是这三家的视频或不是flash地址、embed、iframe代码的 无法解析视频
        _return.code = 0;
        _return.content = "无法正常解析通用代码，请确认通用代码是否完整";
        return _return;
    },
    getEmbedToIframeSrc: function (s, videoFrom) {
        var _regRes = "", _newSrc = '';
        if (videoFrom == "youku") {
            var _srcReg = /.*<embed.+src=['"]([^'"]+)['"]\s.+/gi;
            var _youkuIdReg = /http.+sid\/(.+)\/v\.swf/gi;
            _regRes = this.getEmbedCodeId(s, _srcReg, _youkuIdReg);
            if (_regRes != null && _regRes[1] != "") {
                _newSrc = "http://player.youku.com/embed/" + _regRes[1];
            }
        } else if (videoFrom == "qq") {
            var _srcReg = /.*<embed.+src=['"]([^'"]+)['"]\s.+/gi;
            var _qqIdReg = /http.+&vid=([^&]+).*/gi;
            _regRes = this.getEmbedCodeId(s, _srcReg, _qqIdReg);
            if (_regRes != null && _regRes[1] != "") {
                _newSrc = 'http://v.qq.com/iframe/player.html?vid=' + _regRes[1] + '&tiny=0&auto=0';
            }
        } else if (videoFrom == "qiyi") {
            var _srcReg = /.*<embed.+src=['"]([^'"]+)['"]\s.+/gi;
            var _qiyiIdReg = /http.+qiyi.com\/([^\/]+)\/.+tvId=([^\-]+).+/gi;
            _regRes = this.getEmbedCodeId(s, _srcReg, _qiyiIdReg);
            if (_regRes != null && _regRes[1] != "") {
//                    _newSrc = 'http://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=2e217963dd57fc93b920d5239a60c672&tvId=697063300&height=100%&width=100%';
                _newSrc = 'http://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=' + _regRes[1] + '&tvId=' + _regRes[2] + '&height=100%&width=100%';
            }
        }
        return _newSrc;
    },
    getEmbedCodeId: function (s, _srcReg, _idReg) {
//        var _srcReg = /.*<embed.+src=['"]([^'"]+)['"]\s.+/gi;
        var _srcRegRes = _srcReg.exec(s);
        if (_srcRegRes != null && _srcRegRes[1] != "") {
            var _idRegRes = this.getVideoId(_srcRegRes[1], _idReg);
            if (_idRegRes != null) {
                return _idRegRes;
            }
        }
        return null;
    },
    getVideoId: function (src, _idReg) {
//            var _idReg = /http.+sid\/(.+)\/v\.swf/gi;
        var _idRegRes = _idReg.exec(src);
        return _idRegRes;
    },
    getFlashToIframeSrc: function (src, videoFrom) {
        var _regRes = "", _newSrc = '';
        if (videoFrom == "youku") {
            var _youkuIdReg = /http.+sid\/(.+)\/v\.swf/gi;
            _regRes = this.getVideoId(src, _youkuIdReg);
            if (_regRes != null && _regRes[1] != "") {
                _newSrc = "http://player.youku.com/embed/" + _regRes[1];
            }
        } else if (videoFrom == "qq") {
            var _qqIdReg = /http.+&vid=([^&]+).*/gi;
            _regRes = this.getVideoId(src, _qqIdReg);
            if (_regRes != null && _regRes[1] != "") {
                _newSrc = 'http://v.qq.com/iframe/player.html?vid=' + _regRes[1] + '&tiny=0&auto=0';
            }
        } else if (videoFrom == "qiyi") {
            var _qiyiIdReg = /http.+qiyi.com\/([^\/]+)\/.+tvId=([^\-]+).+/gi;
            _regRes = this.getVideoId(src, _qiyiIdReg);
            if (_regRes != null && _regRes[1] != "") {
//                    _newSrc = 'http://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=2e217963dd57fc93b920d5239a60c672&tvId=697063300&height=100%&width=100%';
                _newSrc = 'http://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=' + _regRes[1] + '&tvId=' + _regRes[2] + '&height=100%&width=100%';
            }
        }
        return _newSrc;
    },
    rotateImg:function(_src,_rotate){
//        if(_rotate > 0 && _rotate < 360){
//            $previewImg.css("transform","rotate("+_rotate+"deg)");
//        }           
        var _imgRotate = _rotate || 0;
        var _newSrc = _src;
        if(_imgRotate > 0 && _imgRotate < 360){
            _newSrc = _src + "?imageMogr2/rotate/" + _imgRotate;
        }        
        return _newSrc;
    }
    
});

/**
 * 初始快速插入内容模块
 * 只用于快速插入类型为6的文本，不用于最后使用
 * @type type
 */
COMS.init = BaseCom.extend({
    type: 0,
    editorId: "hdj-edit-txt",
    ueId: "ue-container",
    init: function () {
        console.log("to init init com.");
    },
    showEditor: function (option) {
        console.log("Enter the init text type edit block coms");
        var editSection = $('#' + this.editorId);
        var _data = {id:option.id,type:option.type,layout:option.layout};
        var _top = $(window).scrollTop() + 120;
        editSection.data("more", _data);
        editSection.show().siblings().hide();
        // editSection.css('top',_top);
        gUeditor = UE.getEditor(this.ueId, {
            autoHeight: true,
            wordCount: false,
            maximumWords: 50000,
            wordOverFlowMsg: "普通文本内容只支持50000个字符"
        });
        gUeditor.ready(function () {
            gUeditor.setContent("");
            gUeditor.focus(true);
        });
    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the init text block coms");
//        if (option.type === this.type && gUeditor !== undefined && gUeditor !== null) {
        if (gUeditor !== undefined && gUeditor !== null) {
            gUeditor.execCommand("selectall");
            gUeditor.execCommand("unlink");
            $(option.preview).children("div.com-content").html(gUeditor.getContent());
        }
        $('#' + this.editorId).hide();
//        if (option.style !== "" && option.style !== undefined && option.style !== null) {
//            option.target.style.cssText = option.style;
//        }
    },
    getEditId:function () {
        return '#' + this.editorId;
    }

});

/**
 * 文本组件
 * @type type
 */
COMS.text = BaseCom.extend({
    type: 6,
    editorId: "hdj-edit-txt",
    ueId: "ue-container",
    init: function () {
        console.log("to init text.");
    },
    showEditor: function (option) {
        console.log("Enter the text type edit block coms");
        var editSection = $('#' + this.editorId);
        var $previewEl = $(option.el);
        var _top = $(window).scrollTop() + 120;
        editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
        editSection.show().siblings().hide();
        // editSection.css('top',_top);
        var _html = $previewEl.children("div.com-content").prop("innerHTML");
        gUeditor = UE.getEditor(this.ueId, {
            autoHeight: true,
            wordCount: false,
            maximumWords: 50000,
            wordOverFlowMsg: "普通文本内容只支持50000个字符"
        });
        gUeditor.ready(function () {
            gUeditor.setContent(_html);
            gUeditor.focus(true);
        });
        rightEditZone.dragEditBox('#' + this.editorId);
    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the text block coms");
        if (option.type === this.type && gUeditor !== undefined && gUeditor !== null) {
            gUeditor.execCommand("selectall");
            gUeditor.execCommand("unlink");            
            $(option.preview).children("div.com-content").html(gUeditor.getContent());
        }
        $('#' + this.editorId).hide();
//        if (option.style !== "" && option.style !== undefined && option.style !== null) {
//            option.target.style.cssText = option.style;
//        }
    },
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        return {
            type: this.type,
            layout: layout,
            data: [
                {
                    t: {
                        t: $.trim($el.children("div.com-content").html()) || "",
                        s: ""
                    },
                    p: {
                        "p": "",
                        "s": ""
                    },
                    s: $el.children("div.com-content").attr('style') || ""
                }
            ],
            s: $el[0].style.cssText
        };
    },
    getEditId:function () {
        return '#' + this.editorId;
    }
});

/**
 * 富文本
 * @type type.text
 */
COMS.html = BaseCom.extend({
    type: 1,
    editorId: "hdj-edit-html",
    ueId: "ue-container-html",
    init: function () {
        console.log("to init html.");
    },
    showEditor: function (option) {
        console.log("Enter the rich text type edit block coms");
        var editSection = $('#' + this.editorId);
        var $previewEl = $(option.el);
        var _top = $(window).scrollTop() + 120;
        editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
        editSection.show().siblings().hide();
        // editSection.css('top',_top);
        var _html = $previewEl.children("div.com-content").prop("innerHTML");
        gHtmlUeditor = UE.getEditor(this.ueId, {
            autoHeight: true
        });
        gHtmlUeditor.ready(function () {
            gHtmlUeditor.setContent(_html);
            gHtmlUeditor.focus(true);
        });
    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the rich text block coms");
        if (option.type === this.type && gHtmlUeditor !== undefined && gHtmlUeditor !== null) {
            gHtmlUeditor.execCommand("selectall");
            gHtmlUeditor.execCommand("unlink");              
            $(option.preview).children("div.com-content").html(gHtmlUeditor.getContent());
        }
        $('#' + this.editorId).hide();
//        if (option.style !== "" && option.style !== undefined && option.style !== null) {
//            option.target.style.cssText = option.style;
//        }
    },    
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        return {
            type: this.type,
            layout: layout,
            data: {
                html: $.trim($el.children("div.com-content").html()) || ""
            },
            s: $el[0].style.cssText
        };
    },
    getEditId:function () {
        return '#' + this.editorId;
    }
});
/**
 * image
 * @type type
 */
COMS.image = BaseCom.extend({
    type: 2,
    init: function () {},
    editId:null,
    showEditor: function (option) {
        console.log("Go to the picture component edit block coms");
        var $previewEl = $(option.el);
        var $editSectionId = $previewEl.attr("id") + "-editor";
        var $editSection = $("#" + $editSectionId);
        var _top = $(window).scrollTop() + 120;
        if (option.cancel === true) {
            $editSection.remove();
            $editSection.length = 0;
        }
        $editSection.remove();//强制重新渲染，之前是允许直接show上次编辑区
        $editSection.length = 0;
        
        if ($editSection.length > 0) {
            $editSection.show().siblings().hide();
        } else {
            var $img = $previewEl.find("div.com-content>img");
            var _src = $img.attr("src");
//            var _text = $previewEl.find("div.com-content>p").text();
            var $imageEditTemplate = $("#hdj-edit-img");
            $editSection = $imageEditTemplate.clone();
            $editSection.attr("id", $editSectionId);
            var $editImg = $editSection.find("img");
            $editImg.attr("src", _src);
            $editImg.css("max-height",this.calculateImgEditorMaxHeight($editImg));
//            $editSection.find("textarea").val(_text);
            $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
            $imageEditTemplate.after($editSection);
            $editSection.show().siblings().hide();
            rightEditZone.dragEditBox("#" + $editSectionId);
            this.editId = $editSectionId;
        }
        // $editSection.css('top',_top);

    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the single graph coms");
        var $editSection = $(option.edit), $previewDiv = $(option.preview);
        if (option.type === this.type && $editSection.length > 0 && $previewDiv.length > 0) {
            var _newSrc = $editSection.children("div.edit-img-content").find("img").attr('src');
//            var _newText = $editSection.children("div.edit-img-content").find("textarea").val();
            var $previewImg = $previewDiv.find("div.com-content>img");
            _newSrc = this.rotateImg(_newSrc,option.setting.img.rotate);
            editRequest.catcheImg2({url:_newSrc},function(result){
                $previewImg.attr("src", result['data'][0]['url']);
            });
//            $previewDiv.find("div.com-content>p").text(_newText);
        }
        $editSection.hide();        
    },
    changeCropImage: function (option) {
        console.log("Single graph edit after finish cropping picture coms");
        var $editImg = $(option.el).parent("div").siblings("img").first();
        $editImg.attr("src", option.url);
        var _that = this;
        $editImg[0].onload = function(){
            $(this).css("max-height",_that.calculateImgEditorMaxHeight($(this)));
        };
    },
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        return {
            type: type,
            layout: layout,
            data: [
                {
                    t: {
                        t: $el.find("div.com-content>p").html() || "",
                        s: $el.find("div.com-content>p").attr('style') || ""
                    },
                    p: {
                        p: $el.find("div.com-content>img").attr('src') || "",
                        s: $el.find("div.com-content>img").attr('style') || ""
                    },
                    s: $el.children("div.com-content").attr('style') || ""
                }
            ],
            s: $el[0].style.cssText
        };
    },
    getEditId:function () {
        return '#' + this.editId;
    }
});
/**
 * images
 * @type type
 */
COMS.images = BaseCom.extend({
    type: 3,
    data:{
        max:20//多图上限20张
    },
    init: function () {},
    editId:null,
    showEditor: function (option) {
        console.log("Enter the multi-map component edit block coms");
        var $previewEl = $(option.el);
        var $editSectionId = $previewEl.attr("id") + "-editor";
        var $editSection = $("#" + $editSectionId);
        var _top = $(window).scrollTop() + 120;
        if (option.cancel === true) {
            $editSection.remove();
            $editSection.length = 0;
        }
        $editSection.remove();//强制重新渲染，之前是允许直接show上次编辑区
        $editSection.length = 0;
        
        if ($editSection.length > 0) {
            $editSection.show().siblings().hide();
        } else {
            var _imageUl = '<ul class="edit-imgs-ul">';
            $previewEl.children("div.com-content").each(function (index, el) {
                var _src = $(this).children("img").attr("src");
                var _text = $(this).children("p").text();
                _imageUl += '<li class="edit-imgs-li">\n\
<div class="edit-imgs-li-drag"><img src="/Public/image/www/editor/corner.png"alt=""></div>\n\
<div class="edit-imgs-li-img"><img src="' + _src + '"alt=""><div class="edit-imgs-delete float-btn">×</div><div class="edit-imgs-bottom float-btn"><div class="edit-imgs-change">替换</div><div class="edit-imgs-crop">裁剪</div></div></div>\n\
<div class="edit-imgs-li-txt"><textarea placeholder="点此可以添加文字描述">' + _text + '</textarea></div>\n\
</li>';
            });
            _imageUl += '</ul>';
            var _imagesEditTemplate = $("#hdj-edit-imgs");
            $editSection = _imagesEditTemplate.clone();
            $editSection.attr("id", $editSectionId);
            $editSection.find("div.edit-imgs-content").prepend(_imageUl);
            $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
            _imagesEditTemplate.after($editSection);
            $editSection.show().siblings().hide();
            rightEditZone.dragEditBox("#" + $editSectionId);
            this.editId = $editSectionId;


        }
        // $editSection.css('top',_top);

    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render multiple graphs coms");
        var $editSection = $(option.edit), $previewDiv = $(option.preview);

        if (option.type === this.type && $editSection.length > 0 && $previewDiv.length > 0) {
            var _new = [];
            $editSection.find("li").each(function (index, el) {
                _new[index] = {src: $(this).children(".edit-imgs-li-img").find("img").attr("src"), text: $(this).children(".edit-imgs-li-txt").find("textarea").val()};
            });
            $previewDiv.children("div.com-content").each(function (index, el) {
                if (_new[index] === undefined) {
                    $(this).children("img").attr("src", "");
                    $(this).children("p").text("");
                } else {
                    $(this).children("img").attr("src", _new[index].src || "");
                    $(this).children("p").text(_new[index].text || "");
                }
            });
        }
        $editSection.hide();
    },
    addNewImages: function (option) {
        console.log("Multi-map editing new picture coms");
        if (option.urls !== undefined || option.urls !== null) {
            var _num = $(option.edit).find("li").length;
            for (var i in option.urls) {
                if(_num >= this.data.max){
                    alert("多图最多支持"+this.data.max+"张图片");
                    return false;
                }
                var _appendLiHtml = '<li class="edit-imgs-li">' +
                        '<div class="edit-imgs-li-drag"><img src="/Public/image/www/editor/corner.png" alt=""></div>' +
                        '<div class="edit-imgs-li-img"><img src="' + option.urls[i] + '" alt=""><div class="edit-imgs-delete float-btn">×</div><div class="edit-imgs-bottom float-btn"><div class="edit-imgs-change">替换</div><div class="edit-imgs-crop">裁剪</div></div></div>' +
                        '<div class="edit-imgs-li-txt"><textarea placeholder="点此可以添加文字描述"></textarea></div>' +
                        '</li>';
                $(option.edit).find("ul").append(_appendLiHtml);
                _num++;
            }
        }
    },
    changeCropImage: function (option) {
        console.log("Multi-map editing after finished cropping picture coms");
        $(option.el).parent("div").siblings("img").first().attr("src", option.url);
    },
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        var multipleImages = {
            type: type,
            layout: layout,
            data: [],
            s: $el[0].style.cssText
        };
        $el.children("div.com-content").each(function (index, el) {
            var tp = {
                t: {
                    t: $(this).find("p").html() || "",
                    s: $(this).find("p").attr('style') || ""
                },
                p: {
                    p: $(this).find("img").attr('src') || "",
                    s: $(this).find("img").attr('style') || ""
                },
                s: this.style.cssText
            };
            multipleImages.data.push(tp);
        });
        return multipleImages;
    },
    getEditId:function () {
        return this.editId;
    }
});
/**
 * dividingline
 * @type type
 */
//COMS.dividingline = BaseCom.extend({
//    type: 7,
//    init: function () {},
//    showEditor: function (option) {
//        console.log("Enter the split line component edit block coms");
//        var $previewEl = $(option.el);
//        var $editSection = $("#hdj-edit-line");
//        if (option.cancel === true) {
//            $("#edit_color_string").val("");
//        }
//        $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
//        $editSection.show().siblings().hide();
//        // rightEditZone.dragEditBox("#hdj-edit-line");
//
//    },
//    reloadPreview: function (option) {
////        {preview:null,edit:null,type:1,layout:1,id:1}
//        console.log("Re-render the split line coms");
//        var $editSection = $(option.edit), $previewDiv = $(option.preview);
//        if (option.type === this.type && $editSection.length > 0 && $previewDiv.length > 0) {
//            var _colorValue = $("#edit_color_string").val();
//            if (_colorValue !== "") {
//                $previewDiv.css({color: _colorValue});
//            }
//        }
//        $editSection.hide();
//    },
//    fetchData: function (option) {
//        var $el = $(option.el);
//        var type = $el.data("type");
//        var layout = $el.data("layout");
//        if (type !== this.type) {
//            return {};
//        }
//        return {
//            type: type,
//            layout: layout,
//            data: [
//                {
//                    t: {
//                        t: "",
//                        s: ""
//                    },
//                    p: {},
//                    s: $el.children("div.com-content").attr("style") || ""
//                }
//            ],
//            s: $el[0].style.cssText
//        };
//    }
//});
/**
 * video
 * @type type
 */
COMS.video = BaseCom.extend({
    type: 8,
    init: function () {},
    editId:null,
    showEditor: function (option) {
        console.log("Enter the video component edit block coms");
        var $previewEl = $(option.el);
        var $editSectionId = $previewEl.attr("id") + "-editor";
        var $editSection = $("#" + $editSectionId);
        var _top = $(window).scrollTop() + 120;
        if (option.cancel === true) {
            $editSection.remove();
            $editSection.length = 0;
        }
        $editSection.remove();//强制重新渲染，之前是允许直接show上次编辑区
        $editSection.length = 0;
        
        if ($editSection.length > 0) {
            $editSection.show().siblings().hide();
        } else {
            var $videoEditTemplate = $("#hdj-edit-video");
            $editSection = $videoEditTemplate.clone();
            $editSection.attr("id", $editSectionId);
            $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout')});
            $videoEditTemplate.after($editSection);
            $editSection.show().siblings().hide();
            //兼容顶部快捷添加video
            var _content = $previewEl.data("content");
            var _input = $previewEl.data("input");
            if (_content !== undefined && _content !== "") {
                if (_input !== undefined && _input !== "") {
                    $editSection.find('.edit-video-textarea').val(_input);
                }else{
                    $editSection.find('.edit-video-textarea').val(_content);
                }
//                $editSection.find('.edit-video-div').html(_content).show();
            }
            // rightEditZone.dragEditBox("#" + $editSectionId);
            this.editId = $editSectionId;

        }
        // $editSection.css('top',_top);

    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the video coms");
        var $editSection = $(option.edit), $previewDiv = $(option.preview);
        if (option.type === this.type && $editSection.length > 0 && $previewDiv.length > 0) {
            var _newText = $.trim($editSection.find("textarea").val());
            $previewDiv.data("input", _newText);
            _newText2 = this.filterSensitiveString(_newText);
            var _res = this.dealVideoSrc(_newText2);
            _newText2 = _res.content;
            if (_res.code === 1) {
                $previewDiv.data("content", _newText2);
                $previewDiv.find(".com-play-icon").attr("src","/Public/image/www/component/com-video2.png");
                //视频预览 暂时不需要
//                if (_newText2 == '') {
//                    $editSection.find('.edit-video-div').hide();
//                } else {
//                    $editSection.find('.edit-video-div').html(_newText2).show();
//                }
                $editSection.find('.edit-video-error').hide();
                $editSection.hide();
                $previewDiv.find('.play-video-text').text('双击可预览视频');

            } else {
                $previewDiv.data("content", "");
                $editSection.find('.edit-video-div').hide();
                $editSection.find('.edit-video-error > span').eq(1).html(_newText2);
                $editSection.find('.edit-video-error').show();
            }
        }
    },
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        var _content = $el.data("content");
        if (_content === undefined || _content === "") {
            return false;
        }
        var videoHtml = this.filterSensitiveString(_content);
        return {
            type: type,
            layout: layout,
            data: [
                {
                    v: {
                        v: videoHtml,
                        s: ""
                    },
                    s: $el.children("div.com-content").attr("style") || ""
                }
            ],
            s: $el[0].style.cssText || ""
        };
    },
    getFirstImage: function (option) {
        html2canvas(option.edit.find('.edit-video-div')[0], {
            onrendered: function (canvas) {
                option.preview.children("div.com-content").prepend(canvas);
            },
            width: 274,
            height: 184
        });
    },
    getEditId:function () {
        return '#' + this.editId;
    }
});
/**
 * timeline
 * @type type
 */
COMS.timeline = BaseCom.extend({
    type: 9,
    init: function () {},
    showEditor: function () {
        console.log("Enter timeline component edit block coms");
    }
});
/**
 * combination
 * @type type
 */
COMS.combination = BaseCom.extend({
    type: 10,
    ueId: "ue-container",
    editorId: "hdj-edit-txt",
    editId:null,
    clickItem:null,
    init: function () {},
    showEditor: function (option) {
        console.log("Into the combination component edit block coms");
        var $previewEl = $(option.el);
        var _top = 0;
        if (gPreviewItemOnclick.el == null) {
            $("#hdj-edit").children("section").hide();
            return true;
        } else {
            this.clickItem = gPreviewItemOnclick.el;
            var $clickItem = $(gPreviewItemOnclick.el);
            if ($clickItem.is("img")) {
                var $editSectionId = $previewEl.attr("id") + "-editor";
                var $editSection = $("#" + $editSectionId);
                if (option.cancel === true) {
                    $editSection.remove();
                    $editSection.length = 0;
                }
                $editSection.remove();//强制重新渲染，之前是允许直接show上次编辑区
                $editSection.length = 0;

                if ($editSection.length <= 0) {
                    if($clickItem.parent("div").hasClass("com-content")){
                        var _src = $clickItem.attr("src");
                    }else{
                        var _src = $clickItem.closest("div.com-item").find("div.com-content > img").attr("src");
                    }
                    var $imageEditTemplate = $("#hdj-edit-img");
                    $editSection = $imageEditTemplate.clone();
                    $imageEditTemplate.after($editSection);
                    $editSection.attr("id", $editSectionId);
                    var $editImg = $editSection.find("img");
                    $editImg.attr("src", _src);
                    $editImg.css("max-height",this.calculateImgEditorMaxHeight($editImg));
    //                $editSection.find("textarea").val(_text);
                    $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout'), el: $clickItem, tag: "img"});
                }else{
                    $editSection.show().siblings().hide();
                }
                _top =  $(window).scrollTop() + 120;

            } else {
                $editSection = $('#' + this.editorId);
                $editSection.data("more", {id: $previewEl.attr('id'), type: $previewEl.data('type'), layout: $previewEl.data('layout'), el: $clickItem, tag: "p"});
                var _html = $clickItem.closest("div.com-content").children("section").prop("innerHTML");
//                UE.delEditor(this.ueId);
                gUeditor = UE.getEditor(this.ueId, {
                    autoHeight: true,
                    wordCount: false,
                    maximumWords: 50000,
                    wordOverFlowMsg: "普通文本内容只支持50000个字符"
                });
                gUeditor.ready(function () {
                    gUeditor.setContent(_html);
                    gUeditor.focus(true);
                });
                _top =  $(window).scrollTop() + 120;
            }
            if (undefined == $editSection) {
                return false;
            }
            $editSection.show().siblings().hide();
//            console.log($editSection[0].id);
//            dragEditBox
//             $('#' + $editSection[0].id).css('top',_top);
            this.editId = $editSection[0].id;
        }
    },
    reloadPreview: function (option) {
//        {preview:null,edit:null,type:1,layout:1,id:1}
        console.log("Re-render the combination coms");
        var $editSection = $(option.edit);
        var _more = $editSection.data("more");
        var $clickItem = _more.el;
        if (_more.tag == 'img') {
            if (option.type === this.type && $editSection.length > 0 && $clickItem.length > 0) {
                var _newSrc = $editSection.children("div.edit-img-content").find("img").attr('src');
                var _newText = $editSection.children("div.edit-img-content").find("textarea").val();
                if($clickItem.parent("div").hasClass("com-content")){
                    _newSrc = this.rotateImg(_newSrc,option.setting.img.rotate);                    
                    editRequest.catcheImg2({url:_newSrc},function(result){
                        $clickItem.attr("src", result['data'][0]['url']);
                    });                    
                    $clickItem.attr("alt", _newText);
                }else{
                    var _clickItemImg = $clickItem.closest("div.com-item").find("div.com-content > img");
                    _newSrc = this.rotateImg(_newSrc,option.setting.img.rotate);                    
                    editRequest.catcheImg2({url:_newSrc},function(result){
                        _clickItemImg.attr("src",result['data'][0]['url']);
                    });                                    
                    _clickItemImg.attr("alt",_newText);
                }
            }
        } else {
            if (option.type === this.type && gUeditor !== undefined && gUeditor !== null) {
                gUeditor.execCommand("selectall");
                gUeditor.execCommand("unlink");                  
                $clickItem.closest("div.com-content").children("section").html(gUeditor.getContent());
            }
        }
        $editSection.hide();
    },
    changeCropImage: function (option) {
        console.log("Graphic single graph edit after finished cropping picture coms");
        var $editImg = $(option.el).parent("div").siblings("img").first();
        $editImg.attr("src", option.url);
        var _that = this;
        $editImg[0].onload = function(){
            $(this).css("max-height",_that.calculateImgEditorMaxHeight($(this)));
        };        
    },   
    fetchData: function (option) {
        var $el = $(option.el);
        var type = $el.data("type");
        var layout = $el.data("layout");
        if (type !== this.type) {
            return {};
        }
        var combination = {
            type: type,
            layout: layout,
            data: [],
            s: $el[0].style.cssText
        };
        $el.children("div.com-content").each(function (index, el) {
            var $section = $(this).find("section");
            var $img = $(this).find("img");
            var tp = {
                t: {
                    t: $section.html() || "",
                    s: $section.attr('style') || ""
                },
                p: {
                    p: $img.attr('src') || "",
                    t: $img.attr('alt') || "",
                    s: $img.attr('style') || ""
                },
                s: this.style.cssText
            };
            combination.data.push(tp);
        });
        return combination;
    },
    getEditId:function () {
        return '#' + this.editId;
    }
});


/**
 * coms factory
 * @param {type} type 组件类型int
 * @returns {COMS}
 */
COMS.factory = function (typeInt) {
    var type = COMS.types[typeInt];
    if (type === undefined || type === null || type === {}) {
        return new BaseCom();
    }
    return new COMS[type];
};




